package cn.myfirefly.core.tag.auth;

import java.io.IOException;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.myfirefly.core.model.Permission;
import cn.myfirefly.utils.StrUtil;

public class ChooseTag extends TagSupport {
	private static Log logger = LogFactory.getLog(ChooseTag.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = -274906636851688051L;

	// 功能编号，例如：customer
	private String funcId;
	
	// 动作，多个时用逗号分隔，例如：/customer/list.do,/customer/mylist.do
	private String actions;
	
	@SuppressWarnings("unchecked")
	public int doStartTag() throws JspException {
		Map<String, Permission> map = (Map<String, Permission>)this.pageContext.getSession().getAttribute("permissions");
		Permission permission = map.get(funcId);
		String[] action = actions.split(",");
		for (int i = 0; i < action.length; i++) {
			if (StrUtil.notBlank(action[i])) {
				if (permission.isPermit(action[i].trim())) {
					try {
						pageContext.getOut().print(action[i].trim());
					} catch (IOException e) {
						logger.error(e.getMessage(), e);
						throw new RuntimeException(e);
					}
					break;
				}
			}
		}
		return SKIP_BODY;
	}

	public String getFuncId() {
		return funcId;
	}

	public void setFuncId(String funcId) {
		this.funcId = funcId;
	}

	public String getActions() {
		return actions;
	}

	public void setActions(String actions) {
		this.actions = actions;
	}
}
